From 395321fa8e96dd0830de8d39aad7d7e0d1b23874 Mon Sep 17 00:00:00 2001 From: "iap10@labyrinth.cl.cam.ac.uk" Date: Wed, 2 Feb 2005 12:34:32 +0000 Subject: [PATCH] bitkeeper revision 1.1159.238.1 (4200c8d8KsGlaM3w6o3y4GHhK1jKjg) A typesafe allocator submitted by Rusty Russel with trivial renames by me. Signed-off-by: Rusty Russel (authored) Signed-off-by: ian.pratt@cl.cam.ac.uk --- xen/arch/x86/irq.c | 2 +- xen/arch/x86/mtrr/generic.c | 3 ++- xen/arch/x86/mtrr/main.c | 3 +-- xen/arch/x86/shadow.c | 3 +-- xen/arch/x86/smpboot.c | 2 +- xen/common/ac_timer.c | 5 ++--- xen/common/dom0_ops.c | 2 +- xen/common/domain.c | 4 ++-- xen/common/event_channel.c | 2 +- xen/common/grant_table.c | 6 +++--- xen/common/physdev.c | 6 +++--- xen/common/resource.c | 2 +- xen/common/sched_atropos.c | 2 +- xen/common/sched_bvt.c | 2 +- xen/drivers/pci/pci.c | 6 +++--- xen/drivers/pci/setup-res.c | 4 ++-- xen/include/xen/slab.h | 12 ++++++++++++ 17 files changed, 38 insertions(+), 28 deletions(-) diff --git a/xen/arch/x86/irq.c b/xen/arch/x86/irq.c index a11ddd3f72..81b8e37d31 100644 --- a/xen/arch/x86/irq.c +++ b/xen/arch/x86/irq.c @@ -260,7 +260,7 @@ int pirq_guest_bind(struct exec_domain *ed, int irq, int will_share) goto out; } - action = xmalloc(sizeof(irq_guest_action_t)); + action = xmalloc(irq_guest_action_t); if ( (desc->action = (struct irqaction *)action) == NULL ) { DPRINTK("Cannot bind IRQ %d to guest. Out of memory.\n", irq); diff --git a/xen/arch/x86/mtrr/generic.c b/xen/arch/x86/mtrr/generic.c index 93d35350a7..6f8d104443 100644 --- a/xen/arch/x86/mtrr/generic.c +++ b/xen/arch/x86/mtrr/generic.c @@ -52,7 +52,8 @@ void __init get_mtrr_state(void) unsigned lo, dummy; if (!mtrr_state.var_ranges) { - mtrr_state.var_ranges = xmalloc(num_var_ranges * sizeof (struct mtrr_var_range)); + mtrr_state.var_ranges = xmalloc_array(struct mtrr_var_range, + num_var_ranges); if (!mtrr_state.var_ranges) return; } diff --git a/xen/arch/x86/mtrr/main.c b/xen/arch/x86/mtrr/main.c index bd1ae5d054..257240b1d1 100644 --- a/xen/arch/x86/mtrr/main.c +++ b/xen/arch/x86/mtrr/main.c @@ -136,8 +136,7 @@ static void __init init_table(void) int i, max; max = num_var_ranges; - if ((usage_table = xmalloc(max * sizeof *usage_table)) - == NULL) { + if ((usage_table = xmalloc_array(unsigned int, max)) == NULL) { printk(KERN_ERR "mtrr: could not allocate\n"); return; } diff --git a/xen/arch/x86/shadow.c b/xen/arch/x86/shadow.c index 0245a05e27..7fd1bbca52 100644 --- a/xen/arch/x86/shadow.c +++ b/xen/arch/x86/shadow.c @@ -176,8 +176,7 @@ int shadow_mode_enable(struct domain *p, unsigned int mode) { struct mm_struct *m = &p->exec_domain[0]->mm; - m->shadow_ht = xmalloc( - shadow_ht_buckets * sizeof(struct shadow_status)); + m->shadow_ht = xmalloc_array(struct shadow_status, shadow_ht_buckets); if ( m->shadow_ht == NULL ) goto nomem; memset(m->shadow_ht, 0, shadow_ht_buckets * sizeof(struct shadow_status)); diff --git a/xen/arch/x86/smpboot.c b/xen/arch/x86/smpboot.c index 5609a118ed..7a98fd8ae5 100644 --- a/xen/arch/x86/smpboot.c +++ b/xen/arch/x86/smpboot.c @@ -409,7 +409,7 @@ void __init start_secondary(void) * At this point, boot CPU has fully initialised the IDT. It is * now safe to make ourselves a private copy. */ - idt_tables[cpu] = xmalloc(IDT_ENTRIES*8); + idt_tables[cpu] = xmalloc_array(struct desc_struct, IDT_ENTRIES); memcpy(idt_tables[cpu], idt_table, IDT_ENTRIES*8); *(unsigned short *)(&idt_load[0]) = (IDT_ENTRIES*8)-1; *(unsigned long *)(&idt_load[2]) = (unsigned long)idt_tables[cpu]; diff --git a/xen/common/ac_timer.c b/xen/common/ac_timer.c index a33498090b..6896e9945a 100644 --- a/xen/common/ac_timer.c +++ b/xen/common/ac_timer.c @@ -130,7 +130,7 @@ static int add_entry(struct ac_timer **heap, struct ac_timer *t) if ( unlikely(sz == GET_HEAP_LIMIT(heap)) ) { int i, limit = (GET_HEAP_LIMIT(heap)+1) << 1; - struct ac_timer **new_heap = xmalloc(limit*sizeof(struct ac_timer *)); + struct ac_timer **new_heap = xmalloc_array(struct ac_timer *, limit); if ( new_heap == NULL ) BUG(); memcpy(new_heap, heap, (limit>>1)*sizeof(struct ac_timer *)); for ( i = 0; i < smp_num_cpus; i++ ) @@ -278,8 +278,7 @@ void __init ac_timer_init(void) for ( i = 0; i < smp_num_cpus; i++ ) { - ac_timers[i].heap = xmalloc( - (DEFAULT_HEAP_LIMIT+1) * sizeof(struct ac_timer *)); + ac_timers[i].heap = xmalloc_array(struct ac_timer *, DEFAULT_HEAP_LIMIT+1); if ( ac_timers[i].heap == NULL ) BUG(); SET_HEAP_SIZE(ac_timers[i].heap, 0); SET_HEAP_LIMIT(ac_timers[i].heap, DEFAULT_HEAP_LIMIT); diff --git a/xen/common/dom0_ops.c b/xen/common/dom0_ops.c index 6d0cbe911d..4611d1f2fe 100644 --- a/xen/common/dom0_ops.c +++ b/xen/common/dom0_ops.c @@ -383,7 +383,7 @@ long do_dom0_op(dom0_op_t *u_dom0_op) if ( op->u.getdomaininfo.ctxt != NULL ) { - if ( (c = xmalloc(sizeof(*c))) == NULL ) + if ( (c = xmalloc(full_execution_context_t)) == NULL ) { ret = -ENOMEM; put_domain(d); diff --git a/xen/common/domain.c b/xen/common/domain.c index 8709630cd4..37e23fc1a6 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -264,7 +264,7 @@ int final_setup_guestos(struct domain *p, dom0_builddomain_t *builddomain) int rc = 0; full_execution_context_t *c; - if ( (c = xmalloc(sizeof(*c))) == NULL ) + if ( (c = xmalloc(full_execution_context_t)) == NULL ) return -ENOMEM; if ( test_bit(DF_CONSTRUCTED, &p->d_flags) ) @@ -311,7 +311,7 @@ long do_boot_vcpu(unsigned long vcpu, full_execution_context_t *ctxt) if ( alloc_exec_domain_struct(d, vcpu) == NULL ) return -ENOMEM; - if ( (c = xmalloc(sizeof(*c))) == NULL ) + if ( (c = xmalloc(full_execution_context_t)) == NULL ) { rc = -ENOMEM; goto out; diff --git a/xen/common/event_channel.c b/xen/common/event_channel.c index bc9a92052d..304172ba26 100644 --- a/xen/common/event_channel.c +++ b/xen/common/event_channel.c @@ -54,7 +54,7 @@ static int get_free_port(struct exec_domain *ed) else max = port + EVENT_CHANNELS_SPREAD; - chn = xmalloc(max * sizeof(event_channel_t)); + chn = xmalloc_array(event_channel_t, max); if ( unlikely(chn == NULL) ) return -ENOMEM; diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index d2aaeec427..e6a76c9dd6 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -565,7 +565,7 @@ grant_table_create( grant_table_t *t; int i; - if ( (t = xmalloc(sizeof(*t))) == NULL ) + if ( (t = xmalloc(grant_table_t)) == NULL ) goto no_mem; /* Simple stuff. */ @@ -573,8 +573,8 @@ grant_table_create( spin_lock_init(&t->lock); /* Active grant table. */ - if ( (t->active = xmalloc(sizeof(active_grant_entry_t) * - NR_GRANT_ENTRIES)) == NULL ) + if ( (t->active = xmalloc_array(active_grant_entry_t, NR_GRANT_ENTRIES)) + == NULL ) goto no_mem; memset(t->active, 0, sizeof(active_grant_entry_t) * NR_GRANT_ENTRIES); diff --git a/xen/common/physdev.c b/xen/common/physdev.c index c815ee4b32..f4d35e3c8b 100644 --- a/xen/common/physdev.c +++ b/xen/common/physdev.c @@ -98,7 +98,7 @@ static void add_dev_to_task(struct domain *p, return; } - if ( (pdev = xmalloc(sizeof(phys_dev_t))) == NULL ) + if ( (pdev = xmalloc(phys_dev_t)) == NULL ) { INFO("Error allocating pdev structure.\n"); return; @@ -174,7 +174,7 @@ int physdev_pci_access_modify( if ( ed->thread.io_bitmap == NULL ) { - if ( (ed->thread.io_bitmap = xmalloc(IOBMP_BYTES)) == NULL ) + if ( (ed->thread.io_bitmap = xmalloc_array(u8, IOBMP_BYTES)) == NULL ) { rc = -ENOMEM; goto out; @@ -765,7 +765,7 @@ void physdev_init_dom0(struct domain *p) if ( (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) && (dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) ) continue; - pdev = xmalloc(sizeof(phys_dev_t)); + pdev = xmalloc(phys_dev_t); pdev->dev = dev; pdev->flags = ACC_WRITE; pdev->state = 0; diff --git a/xen/common/resource.c b/xen/common/resource.c index 8df57772a6..0f479f068b 100644 --- a/xen/common/resource.c +++ b/xen/common/resource.c @@ -220,7 +220,7 @@ int allocate_resource(struct resource *root, struct resource *new, */ struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name) { - struct resource *res = xmalloc(sizeof(*res)); + struct resource *res = xmalloc(struct resource); if (res) { memset(res, 0, sizeof(*res)); diff --git a/xen/common/sched_atropos.c b/xen/common/sched_atropos.c index b5901f2397..32c0090fbb 100644 --- a/xen/common/sched_atropos.c +++ b/xen/common/sched_atropos.c @@ -173,7 +173,7 @@ static int at_alloc_task(struct domain *p) { ASSERT(p != NULL); - p->sched_priv = xmem_cache_alloc(dom_info_cache); + p->sched_priv = xmalloc(struct at_dom_info); if ( p->sched_priv == NULL ) return -1; diff --git a/xen/common/sched_bvt.c b/xen/common/sched_bvt.c index 8e292432a8..59b70ecfc6 100644 --- a/xen/common/sched_bvt.c +++ b/xen/common/sched_bvt.c @@ -557,7 +557,7 @@ int bvt_init_scheduler() for ( i = 0; i < NR_CPUS; i++ ) { - schedule_data[i].sched_priv = xmalloc(sizeof(struct bvt_cpu_info)); + schedule_data[i].sched_priv = xmalloc(struct bvt_cpu_info); if ( schedule_data[i].sched_priv == NULL ) { diff --git a/xen/drivers/pci/pci.c b/xen/drivers/pci/pci.c index 50a4ebb5e0..c3c25b1dba 100644 --- a/xen/drivers/pci/pci.c +++ b/xen/drivers/pci/pci.c @@ -1126,7 +1126,7 @@ static struct pci_bus * __devinit pci_alloc_bus(void) { struct pci_bus *b; - b = xmalloc(sizeof(*b)); + b = xmalloc(struct pci_bus); if (b) { memset(b, 0, sizeof(*b)); INIT_LIST_HEAD(&b->children); @@ -1351,7 +1351,7 @@ struct pci_dev * __devinit pci_scan_device(struct pci_dev *temp) if (l == 0xffffffff || l == 0x00000000 || l == 0x0000ffff || l == 0xffff0000) return NULL; - dev = xmalloc(sizeof(*dev)); + dev = xmalloc(struct pci_dev); if (!dev) return NULL; @@ -1431,7 +1431,7 @@ unsigned int __devinit pci_do_scan_bus(struct pci_bus *bus) max = bus->secondary; /* Create a device template */ - dev0 = xmalloc(sizeof(struct pci_dev)); + dev0 = xmalloc(struct pci_dev); if(!dev0) { panic("Out of memory scanning PCI bus!\n"); } diff --git a/xen/drivers/pci/setup-res.c b/xen/drivers/pci/setup-res.c index 3435b2ac9c..59652dbcc7 100644 --- a/xen/drivers/pci/setup-res.c +++ b/xen/drivers/pci/setup-res.c @@ -171,10 +171,10 @@ pdev_sort_resources(struct pci_dev *dev, struct resource_list *head) ln->res->start; } if (r_align > align) { - tmp = xmalloc(sizeof(*tmp)); + tmp = xmalloc(struct resource_list); if (!tmp) panic("pdev_sort_resources(): " - "xmalloc() failed!\n"); + "malloc() failed!\n"); tmp->next = ln; tmp->res = r; tmp->dev = dev; diff --git a/xen/include/xen/slab.h b/xen/include/xen/slab.h index 692b3b63f3..e08d02b085 100644 --- a/xen/include/xen/slab.h +++ b/xen/include/xen/slab.h @@ -18,6 +18,7 @@ typedef struct xmem_cache_s xmem_cache_t; #include #include +#include /* Flags to pass to xmem_cache_create(). */ /* NB. The first 3 are only valid when built with SLAB_DEBUG_SUPPORT. */ @@ -52,6 +53,17 @@ extern int xmem_cache_reap(void); extern void dump_slabinfo(); +/* Nicely typesafe for you. */ +#define xmalloc(type) ((type *)xmalloc(sizeof(type))) +#define xmalloc_array(type, num) ((type *)xmalloc_array(sizeof(type), (num))) + +static inline void *xmalloc_array(size_t size, size_t num) +{ + /* Check for overflow. */ + if (size && num > UINT_MAX / size) + return NULL; + return xmalloc(size * num); +} #endif /* __ARCH_HAS_SLAB_ALLOCATOR */ #endif /* __SLAB_H__ */ -- 2.30.2